package top.forgirl.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.forgirl.common.result.Result;
import top.forgirl.common.result.ResultType;
import top.forgirl.domain.Login;
import top.forgirl.domain.dto.LoginDo;
import top.forgirl.service.LoginService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@RestController
@Validated
@Api(tags = "登录账号模块")
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录", notes = "返回结果中token为登录的凭证，每次访问接口请带上凭证；limit是所谓的权限，公司中的人" +
            "员才有，提供者这一项为空串")
    @PostMapping("/login")
    public Result login(@Valid @NotNull @RequestBody Login login, HttpServletRequest request) {
        LoginDo loginDo = loginService.login(login);
        if (loginDo == null) {
            return new Result(ResultType.FAILED, "please check principal or credential");
        }
        request.getSession().setAttribute("token", loginDo.getToken());
        return new Result(loginDo);
    }

    @ApiOperation(value = "退出", notes = "退出登录")
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("token") == null) {
            return new Result<>(ResultType.FAILED, "请检查是否已登录");
        }
        session.removeAttribute("token");
        return new Result<>("成功退出");
    }

    @ApiOperation("检验账号是否唯一")
    @GetMapping("/check/{principal}")
    public Result<String> checkUniquePrincipal(@NotBlank(message = "请检查传入的用户账号")
                                               @PathVariable
                                               @Size(max = 11, min = 6, message = "请检查用户名长度") String principal) {
        boolean result = loginService.checkUniquePrincipal(principal);
        if (result) {
            return new Result<>("恭喜，该账号唯一");
        } else {
            return new Result<>("抱歉，该账号已经被占用");
        }
    }

    @ApiOperation("修改登录密码")
    @PutMapping("/login")
    public Result<String> updateLogin(@Valid @NotNull @RequestBody Login login) {
        boolean result = loginService.updateLogin(login);
        if (result) {
            return new Result<>("密码成功修改为" + login.getCredential());
        } else {
            return new Result<>("修改失败");
        }
    }

    @ApiOperation("申请账号")
    @PostMapping("/apply")
    public Result applyLogin(String principal, String credential) {
        Login login = loginService.register(principal, credential);
        if (login != null) {
            return new Result(login);
        }
        return new Result(ResultType.FAILED, "申请失败");
    }

}
